package com.ruoyi.comments.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.education.domain.Comment;
import com.ruoyi.students.domain.Awards;
import com.ruoyi.students.domain.Students;
import com.ruoyi.students.service.IStudentsService;
import com.ruoyi.teacher.domain.Teacher;
import com.ruoyi.teacher.service.ITeacherService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.comments.domain.TecaherComments;
import com.ruoyi.comments.service.ITecaherCommentsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学期评价Controller
 * 功能 : 学期结束老师对学生的评价功能
 * @author yxy
 * @date 2024-10-26
 */
@RestController
@RequestMapping("/comments/comments")
public class TecaherCommentsController extends BaseController
{
    @Autowired
    private ITecaherCommentsService tecaherCommentsService;
    @Autowired
    private IStudentsService studentsService;
    @Autowired
    private ITeacherService teacherService;


    /**
     * 根据studenId查询学期评价列表 ,查询特定学生的评价
     */
    @Anonymous
    @GetMapping(value = "/list/{studentId}")
    public List<TecaherComments> listbystudentid(@PathVariable("studentId") String studentid)
    {
        return  tecaherCommentsService.selectTecaherCommentsListBystudentid(studentid);
    }

    /**
     * 查询学期评价列表
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(TecaherComments tecaherComments)
    {
        startPage();
        List<TecaherComments> list = tecaherCommentsService.selectTecaherCommentsList(tecaherComments);
        return getDataTable(list);
    }

    /**
     * 导出学期评价列表
     */
    @PreAuthorize("@ss.hasPermi('comments:comments:export')")
    @Log(title = "学期评价", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TecaherComments tecaherComments)
    {
        List<TecaherComments> list = tecaherCommentsService.selectTecaherCommentsList(tecaherComments);
        ExcelUtil<TecaherComments> util = new ExcelUtil<TecaherComments>(TecaherComments.class);
        util.exportExcel(response, list, "学期评价数据");
    }

    /**
     * 获取学期评价详细信息
     */
    @PreAuthorize("@ss.hasPermi('comments:comments:query')")
    @GetMapping(value = "/{commentId}")
    public AjaxResult getInfo(@PathVariable("commentId") Long commentId)
    {
        return success(tecaherCommentsService.selectTecaherCommentsByCommentId(commentId));
    }

    /**
     * 新增学期评价
     */
    @PreAuthorize("@ss.hasPermi('comments:comments:add')")
    @Log(title = "学期评价", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TecaherComments tecaherComments)
    {
        return toAjax(tecaherCommentsService.insertTecaherComments(tecaherComments));
    }

    /**
     * 修改学期评价
     */
    @Anonymous
    @PutMapping
    public AjaxResult edit(@RequestBody TecaherComments tecaherComments)
    {
        return toAjax(tecaherCommentsService.updateTecaherComments(tecaherComments));
    }

    /**
     * 删除学期评价
     */
    @PreAuthorize("@ss.hasPermi('comments:comments:remove')")
    @Log(title = "学期评价", businessType = BusinessType.DELETE)
	@DeleteMapping("/{commentIds}")
    public AjaxResult remove(@PathVariable Long[] commentIds)
    {
        return toAjax(tecaherCommentsService.deleteTecaherCommentsByCommentIds(commentIds));
    }

    /**
     * 获取当前老师下的所有学生的评价信息
     */
    @Anonymous
    @GetMapping("/listByTeacherId/{teacherId}")
    public List<TecaherComments> listByTeacherId(@PathVariable("teacherId") String teacherId)
    {
        // 根据teacherid查询评价信息
        return tecaherCommentsService.selectTecaherCommentsListByteacherId(teacherId);
    }

    /**
     * 查询该老师名下的这个学期没有被评价的学生的信息
     */
    @Anonymous
    @GetMapping(value = "/listNoComments/{teacherId}/{termId}")
    public List<Students> listNoComments(@PathVariable("teacherId") String teacherId,@PathVariable("termId") String termId)
    {
        // 根据教师id查询老师信息
        Teacher teacher = new Teacher();
        teacher.setTeacherid(teacherId);
        List<Teacher> teacherlist = teacherService.selectTeacherList(teacher);
        Teacher Thisteacher = teacherlist.get(0);
        // 查询学生
        Students students = new Students();
        students.setStudentclass(Thisteacher.getClassss());
        students.setYearAdmission(Thisteacher.getYearAdmission());
        students.setScchoolMeaasge(Thisteacher.getSchool());
        List<Students> studentlist = studentsService.selectStudentsList(students);

        List<Students> noCommentStudentList = new ArrayList<>();
        // 查看有无评价信息
        for(Students student : studentlist){
            String studentId = student.getStudentId();
            // 根据年级和学号查询
            TecaherComments tecaherComments = tecaherCommentsService.selectTecaherCommentsByCommentIdandDate(studentId,termId);
            if(tecaherComments == null){
                noCommentStudentList.add(student);
            }
        }
        return noCommentStudentList;
    }
}
